package cn.kgc.controller;

import cn.kgc.entities.Emp;
import cn.kgc.service.EmpService;
import cn.kgc.vo.EmpSearch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Controller
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/delay")
    public String testDelay(Model model) {
        //模拟高并发
        ExecutorService pool = Executors.newFixedThreadPool(1000);//线程池最大7000
        for (int i = 0; i < 7000; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    empService.testDelay();
                }
            });
        }
        List<Emp> list = empService.testDelay();
        model.addAttribute("empList", list);
        return "empList";
    }

    @GetMapping("/emp")
    public String getAllEmp(Model model) {
        List<Emp> list = empService.getAllEmp();
        model.addAttribute("empList", list);
        return "empList";
    }

    @GetMapping("/emp/{id}")
    public String getEmpById(@PathVariable("id") Integer id, Model model) {
        Emp emp = empService.getEmpById(id);
        Map<String, Object> sexs = new HashMap<>();
        sexs.put("M", "男");
        sexs.put("F", "女");
        sexs.put("O", "妖");
        model.addAttribute("emp", emp);
        model.addAttribute("sexs", sexs);
        return "emp";
    }

    /**
     * 前往新增界面
     *
     * @param model
     * @return
     */
    @GetMapping("/emp/view")
    public String toAddView(Model model) {
        model.addAttribute("emp", new Emp());
        Map<String, Object> sexs = new HashMap<>();
        sexs.put("M", "男");
        sexs.put("F", "女");
        sexs.put("O", "妖");
        model.addAttribute("sexs", sexs);
        return "emp";
    }

    @PostMapping("/emp")
    public String addEmp(Emp emp) {
        empService.addEmp(emp);
        return "redirect:/emp";
    }

    @PutMapping("/emp")
    public String updateEmp(Emp emp) {
        empService.updateEmp(emp);
        return "redirect:/emp";
    }

    @DeleteMapping("/emp/{id}")
    public String deleteEmp(@PathVariable("id") Integer id) {
        empService.deleteEmpById(id);
        return "redirect:/emp";
    }

    @PostMapping("/emp/search")
    public String searchEmp(EmpSearch empSearch, Model model) {
        List<Emp> list = empService.searchEmp(empSearch);
        model.addAttribute("empList", list);
        return "emplist";
    }

    @ResponseBody
    @GetMapping("/set/{key}/{value}")
    public String getRedisSet(@PathVariable("key") String key, @PathVariable("value") String value) {
        redisTemplate.opsForValue().set(key, value);
        return "set ok";
    }

    @ResponseBody
    @GetMapping("/get/{key}")
    public String getRedisGet(@PathVariable("key") String key) {
        String value = (String) redisTemplate.opsForValue().get(key);
        return value;
    }

}
